from odoo import api, models, fields, _


class SbSubcontractingApply(models.Model):
    _inherit = 'sb.subcontracting.apply'

    def _make_data(self):
        """mes data"""
        data = [{'org_id': '0001',
                 'help_code': self.mes_order,
                 'break_cause': '无',
                 'cancel_by': self.env.user.name,
                 "cancel_date": fields.Datetime.to_string(fields.Datetime.now()),
                 }]
        return {
            "docType": "API_OUT_HELP_CANCEL",
            "updateType": "UPDATE",
            "data": data
        }

    def action_cancel(self):
        res = super().action_cancel()
        mes_enabled = self.env['s.api.config_parameter'].search([('s_key', '=', 'mes.enabled')], limit=1).s_value
        if mes_enabled:
            self.env['s.mes.upload'].upload_data(self._make_data())
        return res
